package cn.dy.workatten.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import cn.dy.base.framework.db.DBConnectPool;
import cn.dy.base.framework.db.DBUtil;
import cn.dy.workatten.pojo.DayModel;

public class WorkDayDao {

	protected static Logger logger = LoggerFactory.getLogger(WorkDayDao.class);

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate;
	private NamedParameterJdbcTemplate namedJdbcTemplate;

	public WorkDayDao() {
		this.dataSource = DBConnectPool.create().getDataSource();
		if (this.jdbcTemplate == null) {
			this.jdbcTemplate = new JdbcTemplate(this.dataSource);
		} if (this.namedJdbcTemplate == null) {
			this.namedJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource);
		}
	}

	public List<DayModel> queryCalendar(String corp_id) {
		String sqlString = "SELECT ID,t.NAME,t.remark,STS,NO_DELETE FROM IP_CALENDAR t WHERE CORP_ID=:CORP_ID ORDER BY t.ID";
		Map<String, Object> paramters20 = new HashMap<String, Object>();
		paramters20.put("CORP_ID", corp_id);
		return this.namedJdbcTemplate.query(sqlString, paramters20, new BeanPropertyRowMapper(DayModel.class));
	}

	public Map<String, Map<String, DayModel>> queryWeekDayForCalendarId(String calendarId) {
		StringBuffer sb = new StringBuffer();
		sb.append(
				"SELECT WDAY_ID,WEEK_TYPE FROM IP_WEEKDAY  WHERE otime_id IS NOT NULL and WDAY_ID IN (SELECT ID FROM IP_WORKDAY WHERE CALENDAR_ID = :CALENDAR_ID)");
		Map<String, Object> paramters20 = new HashMap();
		paramters20.put("CALENDAR_ID", calendarId);
		List<DayModel> list = this.namedJdbcTemplate.query(sb.toString(), paramters20,
				new BeanPropertyRowMapper(DayModel.class));
		Map<String, Map<String, DayModel>> map = new HashMap();
		for (DayModel item : list) {
			if (map.get("week_" + item.getWdayId()) == null) {
				Map<String, DayModel> itemList = new HashMap();
				map.put("week_" + item.getWdayId(), itemList);
			}
			((Map) map.get("week_" + item.getWdayId())).put(item.getWeekType(), item);
		}
		return map;
	}
	
	public DayModel queryForDate(String date, String calId) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ID, CALENDAR_ID, NAME, START_DATE, END_DATE, DAY_TYPE, STS FROM (");
		sb.append("SELECT ID, CALENDAR_ID, NAME, START_DATE, END_DATE, DAY_TYPE, STS FROM IP_WORKDAY");
		sb.append(" WHERE ((TO_CHAR(START_DATE, 'YYYYMMDD') <= :START_DATE ");
		sb.append(" AND :START_DATE <= TO_CHAR(END_DATE, 'YYYYMMDD')) OR (END_DATE IS NULL AND START_DATE IS NULL)) ");
		sb.append(" AND CALENDAR_ID = :CALENDAR_ID AND STS = 1");
		if ("mysql".equals(DBUtil.getDBType(DBConnectPool.create().getDataSource()))) {
			sb.append(" ORDER BY DAY_TYPE DESC,ID DESC ) temp LIMIT 0,1");
		} else {
			sb.append(" ORDER BY DAY_TYPE DESC,ID DESC ) WHERE ROWNUM = 1");
		}
		Map<String, Object> paramters20 = new HashMap();
		paramters20.put("START_DATE", date);
		paramters20.put("CALENDAR_ID", Integer.parseInt(calId));
		return (DayModel) this.namedJdbcTemplate.queryForObject(sb.toString(), paramters20,
				new BeanPropertyRowMapper(DayModel.class));
	}
	
	
	public DayModel queryYearCalendar(int corp_id, String yearStr) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT * FROM ip_calendar where sts = 1 and no_delete = 0 and corp_id=:corp_id and name=:name");
		
		Map<String, Object> params = new HashMap();
		params.put("corp_id", corp_id);
		params.put("name", yearStr);
		return (DayModel) this.namedJdbcTemplate.queryForObject(sb.toString(), params,
				new BeanPropertyRowMapper(DayModel.class));
	}
	
	
	public DayModel queryWeekDay(int wday_id, int week_type) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT * FROM ip_weekday where wday_id=:wday_id and week_type=:week_type");
		
		Map<String, Object> params = new HashMap();
		params.put("wday_id", wday_id);
		params.put("week_type", week_type);
		return (DayModel) this.namedJdbcTemplate.queryForObject(sb.toString(), params,
				new BeanPropertyRowMapper(DayModel.class));
	}

}
